﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Portal.Cms.Model;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Cms.Bussiness;

namespace Portal.Cms.Render
{
    [Serializable]
    public class ScheduleSaveItem
    {
        public int    Day;
        public int SectionTime;
        public string Time="";
        public string ScheduleText="";
    }

    public class AjaxWeeklyScheduleProcess
    {
        public static string GetPlugInJavascript(string SiteId)
        {
            return
                "<script language=javascript>\r\n" +
                "   function ClientShowFormViewWeeklySchedule(WeeklyScheduleId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideGetViewForm(SiteId, WeeklyScheduleId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "       document.getElementById('divActionForm').style.display='block';\r\n" +
                "   }\r\n" +

                "   function ClientShowFormUpdateWeeklySchedule(WeeklyScheduleId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('txtWeeklyScheduleId').value=WeeklyScheduleId;\r\n"+
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       document.getElementById('divActionForm').style.display='none';\r\n" +
                "       document.getElementById('divUpdateForm').style.display='block';\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideCreateWeeklyScheduleObjectItems(SiteId,WeeklyScheduleId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       ScheduleItems=AjaxOut.RetObject;\r\n"+

                "       var RunIndex=0;\r\n" +
                "       for(var iDay=0;iDay<7;iDay++)\r\n" +
                "       {\r\n" +
                "           StartTime = 7;\r\n" +
                "           EndTime = 12;\r\n" +

                "           AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideGetDate(SiteId,WeeklyScheduleId,iDay).value;\r\n" +
                "           if(AjaxOut.Error)\r\n" +
                "           {\r\n" +
                "               alert(AjaxOut.InfoMessage);\r\n" +
                "               return;\r\n" +
                "           }\r\n" +

                "           Ids='HeadingDay'+iDay.toString();\r\n"+
                "           document.getElementById(Ids).innerHTML=AjaxOut.HtmlContent;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n" +
                "           {\r\n" +
                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n"+
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value=ScheduleText;\r\n" +
                
                "               RunIndex++;\r\n" +

                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n" +
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value=ScheduleText;\r\n" +

                "               RunIndex++;\r\n" +
                "           }\r\n" +


                "           StartTime = 12;\r\n" +
                "           EndTime = 17;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n" +
                "           {\r\n" +
                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n" +
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value=ScheduleText;\r\n" +

                "               RunIndex++;\r\n" +

                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n" +
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value=ScheduleText;\r\n" +

                "               RunIndex++;\r\n" +
                "           }\r\n" +

                "           StartTime = 17;\r\n" +
                "           EndTime = 22;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n" +
                "           {\r\n" +
                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n" +
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value=ScheduleText;\r\n" +

                "               RunIndex++;\r\n" +

                //"               iDay=ScheduleItems[RunIndex].DayOfWeek;\r\n" +
                "               ScheduleText=ScheduleItems[RunIndex].ScheduleText;\r\n" +
                "               document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value=ScheduleText;\r\n" +

                "               RunIndex++;\r\n" +
                "           }\r\n" +
                "       }\r\n"+
                "   }\r\n" +

                "   function ClientSaveUpdateWeeklySchedule()\r\n" +
                "   {\r\n" +
                "       WeeklyScheduleId=document.getElementById('txtWeeklyScheduleId').value;\r\n"+
                "       SaveItems = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideGetSaveObjectItem().value;\r\n" +
                "       var RunIndex=0;\r\n"+
                "       for(var iDay=0;iDay<7;iDay++)\r\n" +
                "       {\r\n"+
                "           StartTime = 7;\r\n"+
                "           EndTime = 12;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n"+
                "           {\r\n"+
                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':00';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=0;\r\n"+
                "               RunIndex++;\r\n"+

                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':30';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=0;\r\n" +
                "               RunIndex++;\r\n" +
                "           }\r\n"+

                "           StartTime = 12;\r\n" +
                "           EndTime = 17;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n" +
                "           {\r\n" +
                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':00';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=1;\r\n" +
                "               RunIndex++;\r\n" +

                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':30';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=1;\r\n" +
                "               RunIndex++;\r\n" +
                "           }\r\n" +

                "           StartTime = 17;\r\n" +
                "           EndTime = 22;\r\n" +
                "           for (var iIndexTime = StartTime; iIndexTime < EndTime;iIndexTime++ )\r\n" +
                "           {\r\n" +
                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':00';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-00').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=2;\r\n" +
                "               RunIndex++;\r\n" +

                "               SaveItems[RunIndex].Day=iDay;\r\n" +
                "               SaveItems[RunIndex].Time=iIndexTime+':30';\r\n" +
                "               SaveItems[RunIndex].ScheduleText=document.getElementById('txt-'+iDay+'-'+iIndexTime+'-30').value;\r\n" +
                "               SaveItems[RunIndex].SectionTime=2;\r\n" +
                "               RunIndex++;\r\n" +
                "           }\r\n" +

                "       }\r\n"+

                "       AjaxOut =  Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideSaveUpdateWeeklySchedule(SiteId, WeeklyScheduleId, SaveItems).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       alert(AjaxOut.InfoMessage);\r\n" +
                "       ClientCloseActionForm();\r\n" +
                "   }\r\n" +


                "   function ClientShowFormAddWeeklySchedule(WeeklyScheduleId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideGetAddForm(SiteId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "       document.getElementById('divActionForm').style.display='block';\r\n" +
                "       document.getElementById('txtWeekNumber').focus();\r\n" +
                "   }\r\n" +


                "   function ClientSaveAddWeeklySchedule(WeeklyScheduleId)\r\n" +
                "   {\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideCreateWeeklyScheduleObject(SiteId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       OWeeklySchedule = AjaxOut.RetObject;\r\n" +
                "       OWeeklySchedule.WeekNumber=document.getElementById('txtWeekNumber').value;\r\n" +
                "       OWeeklySchedule.Year=document.getElementById('txtYear').value;\r\n" +
                "       OWeeklySchedule.Description=document.getElementById('txtDescription').value;\r\n" +
                "       AjaxOut =  Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideSaveAddWeeklySchedule(SiteId, OWeeklySchedule).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       alert(AjaxOut.InfoMessage);\r\n" +
                "       ClientCloseActionForm();\r\n" +
                "   }\r\n" +


                "   function ClientDeleteWeeklySchedule(WeeklyScheduleId)\r\n" +
                "   {\r\n" +
                "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideDeleteWeeklySchedule(SiteId, WeeklyScheduleId).value;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       RealClientJumpPage();\r\n" +
                "   }\r\n" +

                "   function ClientCloseActionForm()\r\n" +
                "   {\r\n" +
                "       document.getElementById('divMainForm').style.display='block';\r\n" +
                "       document.getElementById('divActionForm').style.display='none';\r\n" +
                "       document.getElementById('divUpdateForm').style.display='none';\r\n" +
                "       RealClientJumpPage();\r\n"+
                "   }\r\n" +

                "   function ClientJumpPage(CurrentPageIndex)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('txtPageIndex').value=CurrentPageIndex;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='Đang đọc dữ liệu từ máy chủ...';\r\n" +
                "       setTimeout('RealClientJumpPage()',10);\r\n" +
                "   }\r\n" +

                "   function RealClientJumpPage()\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       PageIndex = document.getElementById('txtPageIndex').value;\r\n" +
                "       Keyword = document.getElementById('txtKeyword').value;\r\n" +
                "       AjaxOut = Portal.Cms.Render.AjaxWeeklyScheduleProcess.ServerSideReading(SiteId, Keyword, PageIndex).value;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divWeeklyScheduleContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "   }\r\n" +
                "</script>\r\n";
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReading(string SiteId, string Keyword, string PageIndex)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                WeeklyScheduleCls[] WeeklySchedules = new WeeklyScheduleBll().ReadingWeeklySchedule(SiteId, Keyword);

                string RetHtml =
                 //   "<div class=\"CssRecordFound\">Tìm thấy tổng số (" + WeeklySchedules.Length.ToString("#,##0") + ") vị trí công việc theo điều kiện lọc</div>\r\n" +
                    "<div id=\"divProcessing\"></div>";

                if (WeeklySchedules.Length == 0)
                {
                    RetHtml += "<div class=\"CssRecordFound\">Chưa có dữ liệu nào trong chuyên mục này!</div>";
                }
                else
                {
                    ReturnPaging oReturnPaging = WebPaging.GetPaging(WeeklySchedules.Length, int.Parse(PageIndex), 25, 10, "ClientJumpPage");
                    RetHtml +=
                    oReturnPaging.PagingText +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>\r\n" +
                    "       <tr>" +
                    "           <th class=\"thIndex\">#</th>" +
                    "           <th>Tuần/Năm</th>" +
                    "           <th>Mô tả</th>" +
                    "           <th></th>" +
                    "       </tr>";
                    int StartIndex = oReturnPaging.StartIndex;
                    int EndIndex = oReturnPaging.EndIndex;
                    //int LevelIndex = 0;
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string WeeklyScheduleId = WeeklySchedules[iIndex].WeeklyScheduleId;
                        string WeekNumber = WeeklySchedules[iIndex].WeekNumber;
                        string Year = WeeklySchedules[iIndex].Year;
                        string Ids = "Item" + iIndex;
                        string DelButtonText = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteWeeklySchedule('" + WeeklyScheduleId + "');\">";
                        string UpdateButtonText = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientShowFormUpdateWeeklySchedule('" + WeeklyScheduleId + "');\">";

                        RetHtml += "<tr>" +
                                   "      <td class=\"tdIndex\">" + (iIndex + 1) + "</td>" +
                                   "      <td>" + WeekNumber+"/"+Year + "</td>\r\n" +
                                   "      <td><a href=\"javascript:ClientShowFormViewWeeklySchedule('" + WeeklyScheduleId + "');\">Lịch tuần "+WeekNumber+"/"+Year+"</a></td>" +
                                   "      <td class=\"tdFunction\">\r\n" +
                                            DelButtonText +UpdateButtonText+
                                   "     </td>" +
                                   "</tr>";
                    }
                    RetHtml +=
                        "   </table>\r\n</div>\r\n";
                }
                oAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideCreateWeeklyScheduleObject(string SiteId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                WeeklyScheduleCls OWeeklySchedule = WeeklyScheduleParser.CreateInstance();// new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);
                oAjaxOut.RetObject = OWeeklySchedule;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideCreateWeeklyScheduleObjectItems(
            string SiteId, 
            string WeeklyScheduleId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                int MaxDays=7;
                int MaxHour=30;
                WeeklyScheduleCls OWeeklySchedule = new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);
                WeeklyScheduleItemCls[] WeeklyScheduleItems = WeeklyScheduleItemParser.ParseMultiRecordFromXml(OWeeklySchedule.WeeklyScheduleItemText);
                if (WeeklyScheduleItems.Length == 0)
                {
                    WeeklyScheduleItems = new WeeklyScheduleItemCls[MaxDays * MaxHour];
                    for (int iIndex = 0; iIndex < WeeklyScheduleItems.Length; iIndex++)
                    {
                        WeeklyScheduleItems[iIndex] = new WeeklyScheduleItemCls();
                    }
                }
                oAjaxOut.RetObject = WeeklyScheduleItems;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideSaveUpdateWeeklySchedule(
            string SiteId,
            string WeeklyScheduleId,
            ScheduleSaveItem[] ScheduleSaveItems)
        {
            
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                
                SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
                WeeklyScheduleCls OWeeklySchedule = new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);
                DateTime dFirstDateOfWeek = TouchScreenUtility.FirstDateOfWeek(int.Parse(OWeeklySchedule.Year), int.Parse(OWeeklySchedule.WeekNumber), CalendarWeekRule.FirstDay);
            
                WeeklyScheduleItemCls[] WeeklyScheduleItems = new WeeklyScheduleItemCls[ScheduleSaveItems.Length];
                for (int iIndex = 0; iIndex < ScheduleSaveItems.Length; iIndex++)
                {
                    WeeklyScheduleItems[iIndex] = WeeklyScheduleItemParser.CreateInstance();
                    WeeklyScheduleItems[iIndex].BelongSite = OSiteParam.SiteData;
                    WeeklyScheduleItems[iIndex].DayOfWeek = ScheduleSaveItems[iIndex].Day.ToString();
                    WeeklyScheduleItems[iIndex].frkWeeklyScheduleId = WeeklyScheduleId;
                    WeeklyScheduleItems[iIndex].ScheduleDate = dFirstDateOfWeek.AddDays(iIndex).ToString();
                    WeeklyScheduleItems[iIndex].ScheduleText = ScheduleSaveItems[iIndex].ScheduleText;
                    WeeklyScheduleItems[iIndex].Time = ScheduleSaveItems[iIndex].Time.PadLeft(5,'0');
                    WeeklyScheduleItems[iIndex].ForeColor = "black";
                    WeeklyScheduleItems[iIndex].Segment = ScheduleSaveItems[iIndex].SectionTime.ToString();

                    //WeeklyScheduleItems[iIndex].Time = ScheduleSaveItems[iIndex].Section;
                }
                OWeeklySchedule.WeeklyScheduleItemText = WeeklyScheduleItemParser.GetXml(WeeklyScheduleItems);
                OWeeklySchedule.WeeklyScheduleItems = WeeklyScheduleItems;
                new WeeklyScheduleBll().SaveWeeklySchedule(SiteId, WeeklyScheduleId, OWeeklySchedule);
                oAjaxOut.InfoMessage = "Cập nhật lịch công tác tuần thành công";
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideSaveAddWeeklySchedule(string SiteId, WeeklyScheduleCls OWeeklySchedule)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new WeeklyScheduleBll().AddWeeklySchedule(SiteId, OWeeklySchedule);
                oAjaxOut.InfoMessage = "Thêm mới lịch công tác tuần thành công";
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideDeleteWeeklySchedule(string SiteId, string WeeklyScheduleId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new WeeklyScheduleBll().DeleteWeeklySchedule(SiteId, WeeklyScheduleId);
                oAjaxOut.InfoMessage = "Xoá lịch công tác tuần thành công";
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        static string GetScheduleCol(int iDay, int Time,string Title)
        {
            string RetHtml = "";
                //"<h2>"+Title+"</h2>\r\n";
            int StartTime = 7;
            int EndTime = 12;
            if (Time == 1)
            {
                StartTime = 12;
                EndTime = 17;
            }
            if (Time ==2)
            {
                StartTime = 17;
                EndTime = 22;
            }

            RetHtml += "<table>\r\n";
                       
            for (int iIndex = StartTime; iIndex < EndTime;iIndex++ )
            {
                RetHtml+=
                    "  <tr>";
                string Times = iIndex.ToString().PadLeft(2, '0') + ":00";
                RetHtml += "  <td style=\"width:40px;background-color:whitesmoke;\">" + Times + "</td>\r\n" +
                           "  <td><textarea style=\"width:99%;height:50px;\" id=txt-" + iDay + "-" + iIndex + "-00></textarea></td>\r\n" +
                       "</tr>\r\n";
                Times = iIndex.ToString().PadLeft(2, '0') + ":30";
                RetHtml += "<tr>\r\n"+
                           "  <td style=\"width:40px;background-color:whitesmoke;\">" + Times + "</td>\r\n" +
                           "  <td><textarea style=\"width:99%;height:50px;\" id=txt-" + iDay + "-" + iIndex + "-30></textarea></td>\r\n" +
                           "</tr>\r\n";
            }
            RetHtml+=
                     "</table>\r\n";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideGetUpdateForm(string SiteId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                //WeeklyScheduleCls oWeeklySchedule = new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);
                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td>\r\n" +
                    "           <div class=CssFormSaveUpdate>\r\n" +
                    "           <table>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col2 style=\"text-align:right;\"><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveUpdateWeeklySchedule();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm();\"></td>\r\n" +
                    "           </tr>\r\n";

                //doan nay tab
                string[] BodyTab = new string[7];
                for (int iIndex = 0; iIndex < 7; iIndex++)
                {
                    string Subject = "Nhập lịch cho thứ " + (iIndex + 2) + " - <span id=\"HeadingDay" + iIndex.ToString() + "\"></span>\r\n";
                    if (iIndex == 6) Subject = "Nhập lịch cho chủ nhật <span id=\"HeadingDay" + iIndex.ToString() + "\"></span>\r\n";
                    BodyTab[iIndex] =
                        "<h2 style=\"text-align:center;\">"+Subject+"</h2>\r\n"+
                        "<div>\r\n" +
                        "   <table>\r\n" +
                        "       <tr>\r\n" +
                        "           <td>" + GetScheduleCol(iIndex, 0, "Sáng") + "</td>\r\n" +
                        "       <tr>\r\n"+
                        "       <tr>\r\n"+
                        "           <td>" + GetScheduleCol(iIndex, 1, "Chiều") + "</td>\r\n" +
                        "       </tr>\r\n"+
                        "       <tr>\r\n"+
                        "           <td>" + GetScheduleCol(iIndex, 2, "Tối") + "</td>\r\n" +
                        "       </tr>\r\n" +
                        "   </table>\r\n"+
                        "</div>\r\n";

                }

                string TabText = AjaxTab.GetTab(SiteId, "DaySchedule", new TabItem[]
                    {
                        new TabItem("Day0","Thứ 2",BodyTab[0]),
                        new TabItem("Day1","Thứ 3",BodyTab[1]),
                        new TabItem("Day2","Thứ 4",BodyTab[2]),
                        new TabItem("Day3","Thứ 5",BodyTab[3]),
                        new TabItem("Day4","Thứ 6",BodyTab[4]),
                        new TabItem("Day5","Thứ 7",BodyTab[5]),
                        new TabItem("Day6","Chủ nhật",BodyTab[6]),
                    }, 0);


                Html +=
                    "           <tr>\r\n"+
                    "               <td class=Col2>" + TabText + "</td>\r\n" +
                    "           </tr>\r\n"+
                    "           <tr>\r\n" +
                    "               <td class=Col2 style=\"text-align:right;\"><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveUpdateWeeklySchedule();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm();\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "       </table>\r\n" +
                    "       </div>\r\n" +
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }




        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideGetAddForm(string SiteId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
                Int32 weekNo = ci.Calendar.GetWeekOfYear(
                    System.DateTime.Now,
                    ci.DateTimeFormat.CalendarWeekRule,
                    ci.DateTimeFormat.FirstDayOfWeek
                );

                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td>\r\n" +
                    "       <div class=CssFormSaveUpdate>\r\n" +
                    "       <table>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1>Tuần:</td>\r\n" +
                    "               <td class=Col2><input type=textbox id=txtWeekNumber value=\"" + weekNo + "\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1>Năm:</td>\r\n" +
                    "               <td class=Col2><input type=textbox id=txtYear value=\""+System.DateTime.Now.Year+"\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1>Ghi chú:</td>\r\n" +
                    "               <td class=Col2><input type=textbox id=txtDescription></td>\r\n" +
                    "           </tr>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1></td>\r\n" +
                    "               <td class=Col2><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveAddWeeklySchedule();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm();\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "       </table>\r\n" +
                    "   </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public ScheduleSaveItem[] ServerSideGetSaveObjectItem()
        {
            int MaxDays=7;
            int MaxHour=30;
            ScheduleSaveItem[] ScheduleSaveItems = new ScheduleSaveItem[(MaxDays) * (MaxHour)];
            for (int iIndex = 0; iIndex < ScheduleSaveItems.Length; iIndex++)
            {
                ScheduleSaveItems[iIndex] = new ScheduleSaveItem();
            }
            return ScheduleSaveItems;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideGetViewForm(string SiteId, string WeeklyScheduleId)
        {
            AjaxOut oAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                WeeklyScheduleCls oWeeklySchedule = new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);
                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td>\r\n" +
                    "<div class=CssFormSaveUpdate>\r\n" +
                    " <table>\r\n" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Tuần:</td>\r\n" +
                    "           <td class=Col2>" + oWeeklySchedule.WeekNumber + "</td>\r\n" +
                    "       </tr>\r\n" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Năm:</td>\r\n" +
                    "           <td class=Col2>" + oWeeklySchedule.Year + "</td>\r\n" +
                    "       </tr>\r\n" +
                     "       <tr>\r\n" +
                    "           <td class=Col1>Ghi chú:</td>\r\n" +
                    "           <td class=Col2>" + oWeeklySchedule.Description + "</td>\r\n" +
                    "       </tr>\r\n" +
                     "       <tr>\r\n" +
                    "           <td class=Col1></td>\r\n" +
                    "           <td class=Col2><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm('" + WeeklyScheduleId + "');\"></td>\r\n" +
                    "       </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxOut.Error = true;
                oAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return oAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideGetDate(string SiteId, string WeeklyScheduleId, int iDay)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WeeklyScheduleCls oWeeklySchedule = new WeeklyScheduleBll().CreateWeeklyScheduleModel(SiteId, WeeklyScheduleId);

                string sfd = TouchScreenUtility.FirstDateOfWeek(int.Parse(oWeeklySchedule.Year), int.Parse(oWeeklySchedule.WeekNumber), CalendarWeekRule.FirstDay).AddDays(iDay).ToString("dd/MM/yyyy");
                OAjaxOut.HtmlContent = sfd;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
